package com.fingard.dsp.bank.reqrespobj.batpayrec;

import com.fingard.dsp.bank.reqrespobj.ReqRespHeader;
import com.fingard.dsp.bank.reqrespobj.RetAtsBase;
import com.fingard.dsp.bank.reqrespobj.batpayrec.detail.RetAtsBatPayRecDetail;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.xml.XmlTextWriter;

/**
 * 普通单笔支付提交或查询返回ATS通用对象
 */
public class RetAtsQryBatPayRec extends RetAtsBase {
	public String respCode;
	public String respInfo;
	public String transState;
	public String reqSeqID;
	public String payInfoCode;
	
	public RetAtsQryBatPayRec(ReqRespHeader p_header) {
		retHeader = p_header;
		retXmlWriter = new XmlTextWriter();
	}

	private RetAtsBatPayRecDetail firstDetail = null;
	public void writeRetDetail(RetAtsBatPayRecDetail p_retDetail) throws Exception {
		if (firstDetail == null) {
			firstDetail = p_retDetail;// 用于批量转单笔
		}
		if (!hasWrittenBefore) {
			writeBeforeDetail();
		}
		retXmlWriter.writeStartElement("DetailRecord");
		/*if(retHeader.transType.equals("9288")){
			retXmlWriter.writeElementString("RecAct", p_retDetail.recAct);
			retXmlWriter.writeElementString("RecName", p_retDetail.recName);
			retXmlWriter.writeElementString("TransAmt", p_retDetail.payAmount);
			retXmlWriter.writeElementString("Result", p_retDetail.result);
			retXmlWriter.writeElementString("PostScript", p_retDetail.postScript);
			retXmlWriter.writeElementString("Memo", p_retDetail.memo);
			retXmlWriter.writeElementString("PayInfoCode", p_retDetail.payInfoCode);
			retXmlWriter.writeElementString("TransState", p_retDetail.result);
			retXmlWriter.writeElementString("SuccessAmount", p_retDetail.successAmount);
			retXmlWriter.writeElementString("RespInfo", p_retDetail.memo);
		}else {*/
			if (retHeader.transType.equals("1998")) {
				retXmlWriter.writeElementString("RecAct", p_retDetail.recAct);
				retXmlWriter.writeElementString("RecName", p_retDetail.recName);
			} else if (retHeader.transType.equals("9198") || retHeader.transType.equals("9244")) {
				retXmlWriter.writeElementString("PayAct", p_retDetail.payAct);
				retXmlWriter.writeElementString("PayName", p_retDetail.payName);
				retXmlWriter.writeElementString("SequenceNo", p_retDetail.detailNo);
			}
		  //  retXmlWriter.writeElementString("SequenceNo", p_retDetail.detailNo);
			retXmlWriter.writeElementString("PayAmount", p_retDetail.payAmount);
		    retXmlWriter.writeElementString("SuccessAmount", p_retDetail.successAmount);
		    retXmlWriter.writeElementString("Usage", p_retDetail.usage);
			retXmlWriter.writeElementString("CurType", p_retDetail.curType);
			retXmlWriter.writeElementString("Result", p_retDetail.result);
			retXmlWriter.writeElementString("PostScript", p_retDetail.postScript);
			retXmlWriter.writeElementString("Memo", p_retDetail.memo);
			retXmlWriter.writeElementString("PayInfoCode", p_retDetail.payInfoCode);
			retXmlWriter.writeElementString("ExCur", p_retDetail.exCur);
			retXmlWriter.writeElementString("ExEquvAmt", p_retDetail.exEquvAmt);
			retXmlWriter.writeElementString("ExRate", p_retDetail.exRate);
			retXmlWriter.writeElementString("NewAuthCode", p_retDetail.authCode);
			retXmlWriter.writeElementString("CellPhone", p_retDetail.cellPhone);
            retXmlWriter.writeElementString("BkSeq", p_retDetail.bkSeq);
		    retXmlWriter.writeEndElement();
	}

	private boolean hasWrittenBefore = false;
	public void writeBeforeDetail() throws Exception {
		if (hasWrittenBefore) {
			throw new Exception("writeBeforeDetail已被调用过");
		}
		writeStart();
		retXmlWriter.writeElementString("ReqSeqID", reqSeqID);
		retXmlWriter.writeElementString("RespCode", respCode);
		retXmlWriter.writeElementString("RespInfo", respInfo);
		retXmlWriter.writeElementString("TransState", transState);
		retXmlWriter.writeElementString("PayInfoCode", payInfoCode);
		hasWrittenBefore = true;
	}

	public void writeAfterDetail() throws Exception {
		if (!hasWrittenBefore) {
			writeBeforeDetail();
		}
		writeEnd();// 写结束
	}

	public RetAtsSglDetail convertToSingle() {
		RetAtsSglDetail retDetail = new RetAtsSglDetail();
		if (firstDetail != null) {
			retDetail.reqSeqID = firstDetail.postScript;
			retDetail.respCode = this.respCode;
			retDetail.respInfo = firstDetail.memo;
			retDetail.transState = firstDetail.result;
			retDetail.payInfoCode = firstDetail.payInfoCode;
		} else {
			retDetail.reqSeqID = this.reqSeqID;
			retDetail.respCode = this.respCode;
			retDetail.respInfo = this.respInfo;
			retDetail.transState = this.transState;
			retDetail.payInfoCode = this.payInfoCode;
		}
		return retDetail;
	}
}
